package com.umeox.watch.moto.alarm.ui;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.DateUtils;
import com.umeox.moto.common.utils.LanguageConfig;
import com.umeox.moto.watch.themes.widget.wheel.NumericWheelAdapter;
import com.umeox.moto.watch.themes.widget.wheel.OnWheelScrollListener;
import com.umeox.moto.watch.themes.widget.wheel.WheelView;
import com.umeox.moto.watch.themes.LanguageBean;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.skin.loader.ResourceManager;
import com.umeox.watch.moto.alarm.AlarmDetail;
import com.umeox.watch.moto.alarm.R;
import com.umeox.watch.moto.alarm.adapter.WeekChooseAdapter;
import com.umeox.watch.moto.alarm.base.Constants;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class SetAlarmActivity extends ThemeAppCompatActivity implements View.OnClickListener {

    private WheelView mHourWheel;
    private WheelView mMinuteWheel;
    private ImageView ivAction;
    private int currentHour;
    private int currentMinite;
    public AlarmDetail alarmDetail;
    private int state;
    private ObjectAnimator transAnimation;

    public static final String EXPRESSION = "0000000";
    private WeekChooseAdapter mAdapter;
    private RecyclerView mLanguageRv;
    private WheelView selectWheel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_set_alarm);
        handIntent();

        mLanguageRv = findViewById(R.id.rv_language);

        final Locale[] locales = LanguageConfig.LOCALE_IQIBLA_KID;
        for (Locale value : locales) {
            final LanguageBean languageBean = new LanguageBean();
            languageBean.setLocale(value);
            languageBean.setSelection(Locale.getDefault().equals(value));

        }

        mLanguageRv.setHasFixedSize(true);
        mLanguageRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        ResourceManager res = ResourceManager.getInstance();
        mHourWheel = findViewById(R.id.hourWheel);
        mHourWheel.setValueTextColor(res.getColor(R.color.color_wheel_value_normal));
        mHourWheel.invalidate();
        mMinuteWheel = findViewById(R.id.minuteWheel);
        mMinuteWheel.setValueTextColor(res.getColor(R.color.color_wheel_value_normal));
        mMinuteWheel.invalidate();
        ivAction = findViewById(R.id.ivAction);

        transAnimation = ObjectAnimator.ofFloat(mLanguageRv, "translationX", 0);
        transAnimation.setInterpolator(new LinearInterpolator());
        transAnimation.setDuration(300);

        ivAction.setOnClickListener(this);

        initWheelView();
        initData();
    }

    private void onWheelSelect(WheelView wheel) {
        if (wheel == selectWheel) return;
        ResourceManager res = ResourceManager.getInstance();
        if (selectWheel != null) {
            int colorNormal = res.getColor(R.color.color_wheel_value_normal);
            selectWheel.setValueTextColor(colorNormal);
            selectWheel.invalidate();
        }
        int colorSelected = res.getColor(R.color.color_wheel_value_selected);
        wheel.setValueTextColor(colorSelected);
        wheel.invalidate();

        selectWheel = wheel;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        handIntent();
        initData();
    }

    private void handIntent() {
        alarmDetail = getIntent().getParcelableExtra(Constants.EXTRA_KEY_ALARM);
        if (alarmDetail == null) {
            alarmDetail = new AlarmDetail();
            alarmDetail.setAlarmName(getString(R.string.app_name));
            alarmDetail.setAlarmTime(DateUtils.formatTime(System.currentTimeMillis()));
            alarmDetail.setRepeatExpression(Constants.ALARM_NOREPEAT_EXP);
            alarmDetail.setRingType(0);
            alarmDetail.setState(1);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    public void initWheelView() {
        NumericWheelAdapter arrayAdapter = new NumericWheelAdapter(0, 23, "%02d");
        mHourWheel.setAdapter(arrayAdapter);
        mHourWheel.setCyclic(true);
        mHourWheel.setVisibleItems(3);
        mHourWheel.setCurrentItem(currentHour, false);
        mHourWheel.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                currentHour = wheel.getCurrentItem();
            }
        });
        mHourWheel.setOnTouchListener((View v, MotionEvent event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                onWheelSelect(mHourWheel);
            }
            return v.onTouchEvent(event);
        });

        NumericWheelAdapter arrayAdapter1 = new NumericWheelAdapter(0, 59, "%02d");
        mMinuteWheel.setAdapter(arrayAdapter1);
        mMinuteWheel.setCyclic(true);
        mMinuteWheel.setVisibleItems(3);
        mMinuteWheel.setCurrentItem(currentMinite, false);
        mMinuteWheel.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                currentMinite = wheel.getCurrentItem();
            }
        });
        mMinuteWheel.setOnTouchListener((View v, MotionEvent event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                onWheelSelect(mMinuteWheel);
            }
            return v.onTouchEvent(event);
        });
    }

    List<LanguageBean> mLanguageBeans = new ArrayList<>();

    public void initData() {
        mLanguageBeans.clear();
        String expression = alarmDetail.getRepeatExpression();
        Logger.i("repeat expression data：" + expression);
        state = 0;
        if (mLanguageRv.getTranslationX() == 0) {
            mLanguageRv.setTranslationX(240);
        }
        boolean everyDay = expression.equals(Constants.ALARM_EVERYDAY_REPEAT_EXP);
        LanguageBean everyDayBean = new LanguageBean();
        everyDayBean.setLanguage(getString(R.string.every_day));
        everyDayBean.setSelection(everyDay);
        mLanguageBeans.add(everyDayBean);

        currentHour = alarmDetail.getHour();
        currentMinite = alarmDetail.getMinute();
        mHourWheel.setCurrentItem(currentHour, false);
        mMinuteWheel.setCurrentItem(currentMinite, false);
        String[] weekStr = getResources().getStringArray(R.array.week_days);
        for (int i = 0; i < weekStr.length; i++) {
            String week = weekStr[i];
            LanguageBean languageBean = new LanguageBean();
            languageBean.setLanguage(week);

            if (!everyDay) {
                languageBean.setSelection(expression.charAt(i) == '1');
            }
            mLanguageBeans.add(languageBean);
        }
        mAdapter = new WeekChooseAdapter(mLanguageBeans, null);
        mLanguageRv.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivAction:
                if (state == 0) {
                    state = 1;
                    transAnimation.start();
                } else {

                    alarmDetail.setRepeatExpression(mAdapter.getExpression());
                    Logger.i("repeat expression save：" + alarmDetail.getRepeatExpression());
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY, currentHour);
                    calendar.set(Calendar.MINUTE, currentMinite);
                    alarmDetail.setAlarmTime(DateUtils.formatTime(calendar.getTimeInMillis()));
                    if (alarmDetail.getId() > 0) {
                        alarmDetail.update();
                    } else {
                        alarmDetail.insert();
                    }
                    finish();
                }
                break;
            default:
                break;
        }


    }


}
